package o;

import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.upstream.DataSourceException;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException;
import com.google.android.exoplayer2.upstream.HttpDataSource.InvalidContentTypeException;
import com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException;
import com.google.android.exoplayer2.upstream.HttpDataSource.d;
import com.tencent.mm.opensdk.constants.ConstantsAPI.AppSupportContentFlag;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.NoRouteToHostException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class alp extends alf implements HttpDataSource {
    private static final AtomicReference<byte[]> ˊ = new AtomicReference();
    private static final Pattern ˋ = Pattern.compile("^bytes (\\d+)-(\\d+)/(\\d+)$");
    @Nullable
    private final d ʻ;
    private final d ʼ = new d();
    private final String ʽ;
    private long ˊॱ;
    @Nullable
    private InputStream ˋॱ;
    private final int ˎ;
    private boolean ˏॱ;
    @Nullable
    private DataSpec ͺ;
    private final boolean ॱ;
    @Nullable
    private HttpURLConnection ॱˊ;
    private long ॱˋ;
    private long ॱˎ;
    private final int ॱॱ;
    private long ॱᐝ;
    @Nullable
    private final ang<String> ᐝ;

    public alp(String str, @Nullable ang<String> o_ang_java_lang_String, int i, int i2, boolean z, @Nullable d dVar) {
        super(true);
        this.ʽ = amm.ˋ(str);
        this.ᐝ = o_ang_java_lang_String;
        this.ˎ = i;
        this.ॱॱ = i2;
        this.ॱ = z;
        this.ʻ = dVar;
    }

    @Nullable
    public Uri ˊ() {
        return this.ॱˊ == null ? null : Uri.parse(this.ॱˊ.getURL().toString());
    }

    public Map<String, List<String>> ˎ() {
        return this.ॱˊ == null ? Collections.emptyMap() : this.ॱˊ.getHeaderFields();
    }

    public long ˋ(DataSpec dataSpec) throws HttpDataSourceException {
        long j = 0;
        this.ͺ = dataSpec;
        this.ॱˎ = 0;
        this.ॱˋ = 0;
        ˊ(dataSpec);
        try {
            this.ॱˊ = ॱ(dataSpec);
            try {
                int responseCode = this.ॱˊ.getResponseCode();
                String responseMessage = this.ॱˊ.getResponseMessage();
                if (responseCode < 200 || responseCode > 299) {
                    Map headerFields = this.ॱˊ.getHeaderFields();
                    ᐝ();
                    InvalidResponseCodeException invalidResponseCodeException = new InvalidResponseCodeException(responseCode, responseMessage, headerFields, dataSpec);
                    if (responseCode == 416) {
                        invalidResponseCodeException.initCause(new DataSourceException(0));
                    }
                    throw invalidResponseCodeException;
                }
                responseMessage = this.ॱˊ.getContentType();
                if (this.ᐝ == null || this.ᐝ.ˋ(responseMessage)) {
                    if (responseCode == 200 && dataSpec.ॱॱ != 0) {
                        j = dataSpec.ॱॱ;
                    }
                    this.ˊॱ = j;
                    if (dataSpec.ˊ(1)) {
                        this.ॱᐝ = dataSpec.ᐝ;
                    } else if (dataSpec.ᐝ != -1) {
                        this.ॱᐝ = dataSpec.ᐝ;
                    } else {
                        j = ˎ(this.ॱˊ);
                        this.ॱᐝ = j != -1 ? j - this.ˊॱ : -1;
                    }
                    try {
                        this.ˋॱ = this.ॱˊ.getInputStream();
                        this.ˏॱ = true;
                        ˎ(dataSpec);
                        return this.ॱᐝ;
                    } catch (IOException e) {
                        ᐝ();
                        throw new HttpDataSourceException(e, dataSpec, 1);
                    }
                }
                ᐝ();
                throw new InvalidContentTypeException(responseMessage, dataSpec);
            } catch (IOException e2) {
                ᐝ();
                throw new HttpDataSourceException("Unable to connect to " + dataSpec.ˎ.toString(), e2, dataSpec, 1);
            }
        } catch (IOException e22) {
            throw new HttpDataSourceException("Unable to connect to " + dataSpec.ˎ.toString(), e22, dataSpec, 1);
        }
    }

    public int ˋ(byte[] bArr, int i, int i2) throws HttpDataSourceException {
        try {
            ॱॱ();
            return ˊ(bArr, i, i2);
        } catch (IOException e) {
            throw new HttpDataSourceException(e, this.ͺ, 2);
        }
    }

    public void ॱ() throws HttpDataSourceException {
        try {
            if (this.ˋॱ != null) {
                ˊ(this.ॱˊ, ˋ());
                this.ˋॱ.close();
            }
            this.ˋॱ = null;
            ᐝ();
            if (this.ˏॱ) {
                this.ˏॱ = false;
                ˏ();
            }
        } catch (IOException e) {
            throw new HttpDataSourceException(e, this.ͺ, 3);
        } catch (Throwable th) {
            this.ˋॱ = null;
            ᐝ();
            if (this.ˏॱ) {
                this.ˏॱ = false;
                ˏ();
            }
        }
    }

    protected final long ˋ() {
        return this.ॱᐝ == -1 ? this.ॱᐝ : this.ॱᐝ - this.ॱˎ;
    }

    private HttpURLConnection ॱ(DataSpec dataSpec) throws IOException {
        URL url = new URL(dataSpec.ˎ.toString());
        int i = dataSpec.ॱ;
        byte[] bArr = dataSpec.ˋ;
        long j = dataSpec.ॱॱ;
        long j2 = dataSpec.ᐝ;
        boolean ˊ = dataSpec.ˊ(1);
        if (!this.ॱ) {
            return ˏ(url, i, bArr, j, j2, ˊ, true);
        }
        int i2 = 0;
        while (true) {
            int i3 = i2 + 1;
            if (i2 <= 20) {
                URL ˋ;
                HttpURLConnection ˏ = ˏ(url, i, bArr, j, j2, ˊ, false);
                int responseCode = ˏ.getResponseCode();
                String headerField = ˏ.getHeaderField("Location");
                if ((i == 1 || i == 3) && (responseCode == 300 || responseCode == 301 || responseCode == 302 || responseCode == 303 || responseCode == 307 || responseCode == 308)) {
                    ˏ.disconnect();
                    ˋ = ˋ(url, headerField);
                } else if (i != 2) {
                    return ˏ;
                } else {
                    if (responseCode != 300 && responseCode != 301 && responseCode != 302 && responseCode != 303) {
                        return ˏ;
                    }
                    ˏ.disconnect();
                    i = 1;
                    bArr = null;
                    ˋ = ˋ(url, headerField);
                }
                url = ˋ;
                i2 = i3;
            } else {
                throw new NoRouteToHostException("Too many redirects: " + i3);
            }
        }
    }

    private HttpURLConnection ˏ(URL url, int i, byte[] bArr, long j, long j2, boolean z, boolean z2) throws IOException {
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setConnectTimeout(this.ˎ);
        httpURLConnection.setReadTimeout(this.ॱॱ);
        if (this.ʻ != null) {
            for (Entry entry : this.ʻ.ˊ().entrySet()) {
                httpURLConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
            }
        }
        for (Entry entry2 : this.ʼ.ˊ().entrySet()) {
            httpURLConnection.setRequestProperty((String) entry2.getKey(), (String) entry2.getValue());
        }
        if (!(j == 0 && j2 == -1)) {
            String str = "bytes=" + j + "-";
            if (j2 != -1) {
                str = str + ((j + j2) - 1);
            }
            httpURLConnection.setRequestProperty("Range", str);
        }
        httpURLConnection.setRequestProperty("User-Agent", this.ʽ);
        if (!z) {
            httpURLConnection.setRequestProperty("Accept-Encoding", "identity");
        }
        httpURLConnection.setInstanceFollowRedirects(z2);
        httpURLConnection.setDoOutput(bArr != null);
        httpURLConnection.setRequestMethod(DataSpec.ॱ(i));
        if (bArr != null) {
            httpURLConnection.setFixedLengthStreamingMode(bArr.length);
            httpURLConnection.connect();
            OutputStream outputStream = httpURLConnection.getOutputStream();
            outputStream.write(bArr);
            outputStream.close();
        } else {
            httpURLConnection.connect();
        }
        return httpURLConnection;
    }

    private static URL ˋ(URL url, String str) throws IOException {
        if (str == null) {
            throw new ProtocolException("Null location redirect");
        }
        URL url2 = new URL(url, str);
        String protocol = url2.getProtocol();
        if ("https".equals(protocol) || "http".equals(protocol)) {
            return url2;
        }
        throw new ProtocolException("Unsupported protocol redirect: " + protocol);
    }

    private static long ˎ(HttpURLConnection httpURLConnection) {
        long j = -1;
        String headerField = httpURLConnection.getHeaderField("Content-Length");
        if (!TextUtils.isEmpty(headerField)) {
            try {
                j = Long.parseLong(headerField);
            } catch (NumberFormatException e) {
                amz.ˋ("DefaultHttpDataSource", "Unexpected Content-Length [" + headerField + "]");
            }
        }
        String headerField2 = httpURLConnection.getHeaderField("Content-Range");
        if (TextUtils.isEmpty(headerField2)) {
            return j;
        }
        Matcher matcher = ˋ.matcher(headerField2);
        if (!matcher.find()) {
            return j;
        }
        try {
            long parseLong = (Long.parseLong(matcher.group(2)) - Long.parseLong(matcher.group(1))) + 1;
            if (j < 0) {
                return parseLong;
            }
            if (j == parseLong) {
                return j;
            }
            amz.ˏ("DefaultHttpDataSource", "Inconsistent headers [" + headerField + "] [" + headerField2 + "]");
            return Math.max(j, parseLong);
        } catch (NumberFormatException e2) {
            amz.ˋ("DefaultHttpDataSource", "Unexpected Content-Range [" + headerField2 + "]");
            return j;
        }
    }

    private void ॱॱ() throws IOException {
        if (this.ॱˋ != this.ˊॱ) {
            Object obj = (byte[]) ˊ.getAndSet(null);
            if (obj == null) {
                obj = new byte[4096];
            }
            while (this.ॱˋ != this.ˊॱ) {
                int read = this.ˋॱ.read(obj, 0, (int) Math.min(this.ˊॱ - this.ॱˋ, (long) obj.length));
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedIOException();
                } else if (read == -1) {
                    throw new EOFException();
                } else {
                    this.ॱˋ += (long) read;
                    ˊ(read);
                }
            }
            ˊ.set(obj);
        }
    }

    private int ˊ(byte[] bArr, int i, int i2) throws IOException {
        if (i2 == 0) {
            return 0;
        }
        if (this.ॱᐝ != -1) {
            long j = this.ॱᐝ - this.ॱˎ;
            if (j == 0) {
                return -1;
            }
            i2 = (int) Math.min((long) i2, j);
        }
        int read = this.ˋॱ.read(bArr, i, i2);
        if (read != -1) {
            this.ॱˎ += (long) read;
            ˊ(read);
            return read;
        } else if (this.ॱᐝ == -1) {
            return -1;
        } else {
            throw new EOFException();
        }
    }

    private static void ˊ(HttpURLConnection httpURLConnection, long j) {
        if (anr.ˋ == 19 || anr.ˋ == 20) {
            try {
                InputStream inputStream = httpURLConnection.getInputStream();
                if (j == -1) {
                    if (inputStream.read() == -1) {
                        return;
                    }
                } else if (j <= AppSupportContentFlag.MMAPP_SUPPORT_XLSX) {
                    return;
                }
                String name = inputStream.getClass().getName();
                if ("com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream".equals(name) || "com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream".equals(name)) {
                    Method declaredMethod = inputStream.getClass().getSuperclass().getDeclaredMethod("unexpectedEndOfInput", new Class[0]);
                    declaredMethod.setAccessible(true);
                    declaredMethod.invoke(inputStream, new Object[0]);
                }
            } catch (Exception e) {
            }
        }
    }

    private void ᐝ() {
        if (this.ॱˊ != null) {
            try {
                this.ॱˊ.disconnect();
            } catch (Throwable e) {
                amz.ˊ("DefaultHttpDataSource", "Unexpected error while disconnecting", e);
            }
            this.ॱˊ = null;
        }
    }
}
